import Vue from "vue";
import Router from "vue-router";
import {
    getStorageToken
} from "@/utils/storage.js"

import Home from '@/views/home/Home.vue'

import {
    Dialog
} from "vant";

Vue.use(Router);

const routes = [
    { //定义重定向路由
        path: "/",
        redirect: "/home",
        meta: {
            title: "首页"
        }
    },
    {
        path: "/home", //路由的路径
        name: "home", //路由的名字
        component: Home, //路由组件
        meta: {
            title: "首页",
            keepAlive: true //是否缓存
        }
    },

    { //定义商品列表路由
        path: '/goodsList',
        name: 'goodsList',
        component: () => import( /* webpackChunkName: "goods" */ '@/views/goods/GoodsList.vue'),
        meta: {
            title: "商品",
            keepAlive: true, //是否缓存
        }
    },

    { //定义商品详情路由
        path: '/goodsDetail/:id',
        name: 'goodsDetail',
        component: () => import( /* webpackChunkName: "goods" */ '@/views/goods/GoodsDetail.vue'),
        meta: {
            title: "商品详情"
        }
    },

    { //定义购物车路由
        path: '/cart',
        name: 'cart',
        component: () => import( /* webpackChunkName: "cart" */ '@/views/cart/Cart.vue'),
        meta: {
            title: "购物车",
            alive: true, //是否缓存
            requireAuth: true, //这个路由需要认证
        }
    },

    { //定义我的页面路由
        path: '/user',
        name: 'user',
        component: () => import( /* webpackChunkName: "user" */ '@/views/user/User.vue'),
        meta: {
            title: "我的",
            alive: true, //是否缓存
        }
    },


    { //定义登录路由
        path: '/login',
        name: 'login',
        component: () => import( /* webpackChunkName: "user" */ '@/views/user/Login.vue'),
        meta: {
            title: "登录"
        }
    },

    { //定义注册路由
        path: '/register',
        name: 'register',
        component: () => import( /* webpackChunkName: "user" */ '@/views/user/Register.vue'),
        meta: {
            title: "注册"
        }
    },

    { //个人资料路由
        path: '/profile',
        name: 'profile',
        component: () => import( /* webpackChunkName: "user" */ '@/views/user/Profile.vue'),
        meta: {
            title: "个人资料"
        }
    },

    { //修改密码路由
        path: "/updatePwd",
        component: () => import( /* webpackChunkName: "user" */ "@/views/user/UpdatePwd.vue"),
        name: "updatePwd",
        meta: {
            title: "修改密码"
        }
    },


    { //定义订单创建路由
        path: '/orderCreate',
        name: 'orderCreate',
        component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderCreate.vue'),
        meta: {
            title: "新建订单"
        }
    },
    { //定义订单列表路由
        path: '/orderList',
        name: 'orderList',
        component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderList.vue'),
        meta: {
            title: "订单列表"
        }
    },
    { //定义订单明细路由
        path: '/orderDetail/:id', //动态路由参数
        props: true, //允许props接收
        name: 'orderDetail',
        component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderDetail.vue'),
        meta: {
            title: "订单详情"
        }
    },

    { //定义地址列表路由
        path: '/addressList',
        name: 'addressList',
        component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressList.vue'),
        meta: {
            title: "地址列表"
        }
    },

    { //定义地址编辑路由
        path: '/addressEdit/:id', //动态路由参数
        props: true, //允许props接收
        name: 'addressEdit',
        component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressEdit.vue'),
        meta: {
            title: "地址编辑"
        }
    },

    { //定义新闻列表路由
        path: '/newsList',
        name: 'newsList',
        component: () => import( /* webpackChunkName: "news" */ '@/views/news/NewsList.vue'),
        meta: {
            title: "新闻列表",
        }
    },
    { //定义新闻详情路由
        path: '/newsDetail/:id', //动态路由参数
        props: true, //允许props接收
        name: 'newsDetail',
        component: () => import( /* webpackChunkName: "news" */ '@/views/news/NewsDetail.vue'),
        meta: {
            title: "新闻详情",
        }
    },

    {
        path: "/working",
        component: () => import("@/views/Working.vue"),
        name: "working",
        meta: {
            title: "努力中...."
        },
    },

    {
        path: "*",
        name: "notFound",
        component: () => import( /* webpackChunkName: "notfound" */ '@/views/NotFound.vue'),
        meta: {
            title: "导航页面不存在"
        },
    },
];

let router = new Router({
    // 解决路由跳转页面没有置顶
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {
                x: 0,
                y: 0,
            };
        }
    },
    routes,
});

//设置全局路由导航守卫
router.beforeEach((to, from, next) => {

    if (to.meta.title) {
        document.title = to.meta.title;
    }

    if (to.name === 'login') { // 路由条状到login的，直接放行
        console.log(`${from.name}跳转到login`); // 控制台调试输出
        next(); // 路由继续往下跳转
        return;
    }

    if (!to.meta.requireAuth) { // 路由不需要验证的，直接放行
        console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
        next();
        return;
    }

    // 路由需要验证的
    const token = getStorageToken();
    if (!token) { // 如果token不存在的，则跳转登录login
        console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`);

        Dialog.confirm({
            message: "没有Token,需要重新登录吗?",
        }).then(() => {
            next({
                name: 'login',
                params: {
                    redirect: to.path
                }
            });
        }).catch(() => {
            router.push({
                name: "home"
            });
        });
        return;
    }

    console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`);
    // 其余情况，则路由放行
    next(); //
});


const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

export default router;
